﻿using MaiPlanServer.Dto;
using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IOrganizationaServices;
using MaiPlanServer.Model;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MaiPlanServer.Services.OrganizationalServices
{
    public class UserManageServices : IUserManageServices
    {
        public UserManageServices(IBaseRepository BaseConn)
        {
            this.BaseConn = BaseConn;
        }

        public IBaseRepository BaseConn { get; }

        public async Task<bool> AddAsync(sys_user model)
        {
            var result = await BaseConn.InsertAsync(model);
            return result;
        }

        public Task<bool> DelAllAsnc(string user_id)
        {
            throw new System.NotImplementedException();
        }

        public Task<bool> DelAsync(string user_id)
        {
            throw new System.NotImplementedException();
        }

        public async Task<int> GetUserCount(string nick_name, string phonenumber, string status)
        {
            var sql = "select count(1) from sys_user where 1=1";
            if (!string.IsNullOrEmpty(nick_name))
            {
                sql += " and nick_name like @nick_name";
            }
            if (!string.IsNullOrEmpty(phonenumber))
            {
                sql += " and phonenumber = @phonenumber";
            }
            if (!string.IsNullOrEmpty(status))
            {
                sql += " and status=@status";
            }
            var count = await BaseConn.GetRecoreCountAsync(sql,new { nick_name = "%" + nick_name + "%", phonenumber = phonenumber, status = status });
            return count;
        }

        public async Task<IEnumerable<sys_userDto>> GetUsers(int PageIndex, int PageSize, string nick_name, string phonenumber, string status)
        {
            var sql = "select u.*,d.dept_name from sys_user u left join sys_dept d on u.dept_id=d.dept_id where 1=1";
            if (!string.IsNullOrEmpty(nick_name))
            {
                sql += " and nick_name like @nick_name";
            }
            if (!string.IsNullOrEmpty(phonenumber))
            {
                sql += " and phonenumber = @phonenumber";
            }
            if (!string.IsNullOrEmpty(status))
            {
                sql += " and status=@status";
            }
            sql += " order by Create_Time desc offset @PageSize*(@PageIndex-1) rows fetch next @PageSize row only";
            var list = await BaseConn.QueryAsync<sys_userDto>(sql,new { PageIndex= PageIndex, PageSize = PageSize , nick_name ="%"+ nick_name +"%", phonenumber = phonenumber , status = status });
            return list;
        }

        public Task<bool> PutAsync(sys_user model)
        {
            throw new System.NotImplementedException();
        }
    }
}
